package vn.edu.hut.soict.translate;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import vn.edu.hut.soict.translate.wordgraph.DefaultWord;
import vn.edu.hut.soict.translate.wordgraph.Word;
import vn.hus.nlp.tokenizer.DefaultTokenizerFactory;
import vn.hus.nlp.tokenizer.Token;
import vn.hus.nlp.tokenizer.Tokenizer;
import edu.stanford.nlp.ling.WordTag;

public class DefaultSentenceSplitter implements SentenceSplitter {
	private static final String DEFAULT_RESOURCE_DIRECTORY = "res";
	private DefaultTokenizerFactory tokenizerFactory;
	private POSTagger tagger;

	public DefaultSentenceSplitter() {
		try {
			tokenizerFactory = new DefaultTokenizerFactory(DEFAULT_RESOURCE_DIRECTORY);
			tagger = new POSTagger(DEFAULT_RESOURCE_DIRECTORY);
		}
		catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public Word[] split(String s) {
		// Tokenize
		Tokenizer tokenizer = tokenizerFactory.getTokenizer(s);
		List<String> wordTokens = new ArrayList<String>();
		while (tokenizer.hasNext()) {
			Token token = tokenizer.next();
			String text = token.getText();
			wordTokens.add(text);
		}
		
		// Tagging
		try {
			List<WordTag> taggedWords = tagger.tagList(wordTokens);
			List<Word> words = new ArrayList<Word>();
			for (WordTag taggedWord : taggedWords) {
				words.add(new DefaultWord(taggedWord.value(), taggedWord.tag()));
			}
			return words.toArray(new Word[words.size()]);
		}
		catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;
	}

}
